//
//  MPlabel.m
//  Feeds2
//
//  Created by cos on 2020/11/28.
//  Copyright © 2020 cos. All rights reserved.
//

#import "MPlabel.h"

@interface MPlabel ()

{
    CGPoint _origin;    //原点
    CGPoint _center;    //中心点
    CGFloat _margin;    //空白边界
    int initPoint;      //判断标志，判断当前frame根据那种规则设定
    NSMutableString *_mutableText;  //标签文字内容
    UIFont *_mutableFont;           //标签文字字体
}

@end

@implementation MPlabel

- (instancetype)init {
    
    if (self = [super init]) {
        self.frame = CGRectMake(10, 10, 10, 10); //预设，可随意设置，后面的方法会重新设置
        self.numberOfLines = 0;
        self.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

- (void)awakeFromNib {
    
    self.frame = CGRectMake(10, 10, 10, 10);
    self.numberOfLines = 0;
    self.textAlignment = NSTextAlignmentCenter;
    
    [super awakeFromNib];
}

- (void)setLabelWithOrigin:(CGPoint)origin mutableText:(NSString *)text mutableFont:(UIFont *)font margin:(CGFloat)margin {
    /*int w = [text sizeWithAttributes:@{NSFontAttributeName:font}].width;
    int tem = w;
    w /= 300;
    //w += 1;
    
    int len = (int)text.length;
    
    initPoint = 1; //1表示设定了原点
    
    _origin = origin;
    _margin = margin > 0 ? margin : 0;
    _mutableText = [text mutableCopy];
    _mutableFont = font;
    w = w > 0 ? w:1;
    
    for(int i = len/w;i < (int)text.length + len/w;i += len/w){
        if(i< _mutableText.length-1 ){
            [_mutableText insertString:@"\n" atIndex:i];
        }
    }
    [self setTitle];*/
    int w = [text sizeWithAttributes:@{NSFontAttributeName:font}].width;
    int tem = w;
    w /= 300;
    int len = (int)text.length;
    len = len*300/[text sizeWithAttributes:@{NSFontAttributeName:font}].width;
    initPoint = 1; //1表示设定了原点
    _origin = origin;
    _margin = margin > 0 ? margin : 0;
    _mutableText = [text mutableCopy];
    _mutableFont = font;
    w = w > 0 ? w:1;
    
    for(int i = len;i < (int)text.length + len;i += len){
        if(i< _mutableText.length-1 ){
            [_mutableText insertString:@"\n" atIndex:i];
        }
    }
    [self setTitle];
}

- (void)setLabelWithCenter:(CGPoint)center mutableText:(NSString *)text mutableFont:(UIFont *)font margin:(CGFloat)margin {
    int w = [text sizeWithAttributes:@{NSFontAttributeName:font}].width;
    w /= 300;
    int len = (int)text.length;
    
    initPoint = 2; //2表示设定了中心点
    
    _center = center;
    _margin = margin > 0 ? margin : 0;
    _mutableText = [text mutableCopy];
    _mutableFont = font;
    
    for(int i = len/w;i < (int)text.length;i += len/w){
        [_mutableText insertString:@"\n" atIndex:i];
    }
    [self setTitle];

}

- (void)setTitle {
    
    NSDictionary *textAttributes;
    
    textAttributes = @{NSFontAttributeName:_mutableFont};
    
    CGSize textSize = [_mutableText sizeWithAttributes:textAttributes];
    
    //这里的margin加在了宽度两侧，如果要做竖排标签，可以选择加在高度两侧，也可以再添加一个新的margin参数来同时控制宽度和高度上的空白边界
    if (initPoint == 1) {
        self.frame = CGRectMake(_origin.x, _origin.y, textSize.width + 2*_margin, textSize.height);
    } else if (initPoint == 2) {
        self.center = _center;
        self.bounds = CGRectMake(0, 0, textSize.width + 2*_margin, textSize.height);
    }
    
    [self setText:_mutableText];
    [self setFont:_mutableFont];
}

@end
